import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const store =new Vuex.Store({
    state: {
         isShowShopCar:false, //商品详情页面购物车的显示和隐藏
         count:1 , //购物车的商品数量
         shopCarList:[],//购物车length
         deal:{
            name:"水平有限螺蛳粉",
            img:"../../../static/images/market/commodity/appliances-07.jpg",
            desc:"水平有限 | 螺蛳粉柳州正宗螺蛳粉 320g*3袋/5袋",
            price:47.90,
            count:1,
            size:"320g*5袋",
         },
         total:0,
         shopInfo:[{
            id:1,
            storeName:"叁幸库",
            intro:"叁幸库丨魔力吸水环保清洁布4片装（建议3个月一更换，4片可以用一年）",
            type:"魔力吸水清洁布8片装",
            Newprice:59,
            Oldprice:69,
            num:1,
            url:"../../../static/images/market/commodity/appliances-07.jpg",
            bol:false
        },{
            id:2,
            storeName:"叁幸库",
            intro:"叁幸库丨魔力吸水环保清洁布4片装（建议3个月一更换，4片可以用一年）",
            type:"魔力吸水清洁布8片装",
            Newprice:59,
            Oldprice:69,
            num:1,
            url:"../../../static/images/market/commodity/appliances-07.jpg",
            bol:false
        }],
        ShopTotal:0
        
        },       
    mutations: {
        addcom(state){
            return state.deal.count++
        },
        redcom(state){
            if(state.deal.count<=0){
                state.deal.count=0
            }else{
                return state.deal.count--
            }
        },
        changeNum(state,what){
            // console.log(what);
            if(what[0]=="+"){
                state.shopInfo[what[1]-1].num+=1
            }else{
                if(state.shopInfo[what[1]-1].num-1<=0){
                    state.shopInfo[what[1]-1].num=0
                }else{
                    state.shopInfo[what[1]-1].num-=1
                }
            }
        },
        changeBol(state,id){
            console.log(state.shopInfo[id-1]);
            state.shopInfo[id-1].bol=!state.shopInfo[id-1].bol;
        },
        // 计算总价
        getTotal(state,id){
            state.ShopTotal=0;
            for(var i in state.shopInfo){
                if(state.shopInfo[i].bol){
                    state.ShopTotal+=parseInt(state.shopInfo[i].num)*parseInt(state.shopInfo[i].Newprice)
                }
            }
            state.ShopTotal=state.ShopTotal*100
        }
    },
    getters:{
        totalPrice(state){
            return state.total=state.deal.count*state.deal.price
        }
    }   //修改state
    // actions: {},    //异步操作
    // getters:{},     //计算属性
    // modules: {}     //模块
    })
    export {store}